﻿#region Namespace Imports


// Standard class imports
using System;
using System.Collections.Generic;
using System.Text;

// Supports logging operations
using System.Diagnostics;
using System.Globalization;

// Eases referencing of HttpContext type
using System.Web;

// Alias the assembly resources to shorten references.
using res = SPMcDonough.Moss.BlobCacheFarmFlush.Properties.Resources;


#endregion Namespace Imports


namespace SPMcDonough.Moss.BlobCacheFarmFlush
{


    /// <summary>
    /// This utility class is used as a "common area" for any methods, constants, and
    /// other items/types that are used across classes and should be centralized.
    /// </summary>
    internal static class Globals
    {


        #region Methods (Public, Static)


        /// <summary>
        /// This method is used to centralize attempts to write to the Windows Event
        /// Log.  As activation, deactivation, installation, and uninstallation occur,
        /// status messages (and exceptions, if required) are written to the log in
        /// order to provide an indication of operational success or failure.
        /// </summary>
        /// <param name="eventMessage">A string that contains the fully formatted and
        /// log-ready message that will be written out.</param>
        /// <param name="eventId">A string containing the event ID that will be logged.
        /// This string is converted to an Int32 in order to write it to the event log.</param>
        /// <param name="eventType">A member of the EventLogEntryType enumeration that
        /// identifies the type of entry being logged (error, warning, or info).</param>
        public static void LogEvent(String eventMessage, String eventId, EventLogEntryType eventType)
        {
            // Attempt logging operations inside of an exception handler on the off-chance
            // that a problem is encountered actually writing to the log.
            try
            {
                // EventLog types implement IDisposable, so we need to properly release
                // resources when finished.
                using (EventLog logSink = new EventLog(res.LOG_LOGNAME, res.LOG_MACHINENAME, res.LOG_SOURCE))
                {
                    // Write the desired message and ID to the log.
                    logSink.WriteEntry(eventMessage, eventType, Int32.Parse(eventId, CultureInfo.InvariantCulture));
                }
            }

            catch (Exception ex)
            {
                // A problem was encountered actually writing to the log.  This is an odd
                // thing, but we'll attempt to communicate the problem to the user or bubble
                // the exception up.
                //
                // Prepare a wrapper for the exception that was thrown.
                Exception wrappedException = new InvalidOperationException(res.FORMAT_EX_EVENT_LOG_WRITE_FAIL, ex);

                // Do we have a valid HttpContext?  If so, we'll bolt the exception message
                // to the collection of errors.  If not, we'll simply re-throw and hope for
                // the best ...
                if (HttpContext.Current != null)
                {
                    // We have an HttpContext, so we'll bolt the exception onto it.  This 
                    // is a relatively safe way of bubbling the exception out in a way that
                    // ASP.NET will be able to process, handle, and communicate.
                    HttpContext.Current.AddError(wrappedException);
                }

                else
                {
                    // No HttpContext; simply throw the wrapped exception.
                    throw wrappedException;
                }
            }
        }


        #endregion Methods (Public, Static)


    }
}
